<?php

declare(strict_types=1);

namespace App\Controller\Kpi;

use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\Validation\Annotation\Scene;
use App\Service\Banner as BannerService;
use App\Request\Banner as BannerRequest;

#[AutoController]
class BannerController extends AbstractController
{
    // 广告列表
    public function list()
    {
        $map = [];
        $title = $this->request->input('title', '');
        if ($title) {
            $map[] = ['title', 'like', '%' . $title . '%'];
        }
        
        $key = $this->request->input('key', '');
        if ($key) {
            $map[] = ['key', '=', $key];
        }
        
        $status = $this->request->input('status', -1);
        if ($status > -1) {
            $map[] = ['status', '=', $status]; 
        }
        
        $per_page = $this->request->input('per_page', 10);
        $result = (new BannerService())->list($map,(int)$per_page);
        return $this->returnSuccess($result, '成功');
    }
    // 添加广告
    #[Scene(scene: 'add', argument: 'request')]
    public function add(BannerRequest $request)
    {
        $params = $request->validated();
        $result = (new BannerService())->addAdver($params);
        return $this->returnSuccess($result, '保存成功');
    }

    // 编辑广告
    #[Scene(scene: 'edit', argument: 'request')]
    public function edit(BannerRequest $request)
    {
        $params = $request->validated();
        $result = (new BannerService())->editAdver($params['id'], $params);
        return $this->returnSuccess($result, '保存成功');
    }

    // 删除广告
    public function delete()
    {
        $id = $this->request->input('id', 0);
        if (!$id)
            return $this->returnError('请求参数错误');
        $data = (new BannerService())->deleteAdver($id);
        return $this->returnSuccess($data, '操作成功');
    }
}
